package com.ellricky.baselib.ui;

import android.widget.TextView;

import com.andr.common.ui.activity.BaseToolBarActivity;
import com.andr.common.util.EmptyUtil;
import com.andr.common.util.ToastUtil;
import com.ellricky.baselib.BaseConstant;
import com.ellricky.baselib.R;
import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
import com.github.barteksc.pdfviewer.scroll.DefaultScrollHandle;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


/**
 * Created by april on 16/11/10.
 */

public class PdfViewerActivity extends BaseToolBarActivity {
    private PDFView mPdfView;
    private TextView mPageTv;

    private int pageNumber;
    private String pdfUrl;

    @Override
    public void initUIView() {
        super.initUIView();
        mTvTitle.setText("文件查看");
        mPdfView = findViewById(R.id.pdf_view);
        mPageTv = findViewById(R.id.tv_page);

        pdfUrl = getIntent().getStringExtra(BaseConstant.KEY_URL);
        if (!EmptyUtil.isEmpty(pdfUrl)) {
            loadPad(mPdfView, pdfUrl);
        }else {
            ToastUtil.show(this,"文件地址为空");
        }

    }

    @Override
    public int getContentLayout() {
        return R.layout.base_activity_pdf_viewer;
    }


    public void loadPad(final PDFView pdfview, final String pdfUrl) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url = new URL(pdfUrl);
                    HttpURLConnection connection = (HttpURLConnection)
                            url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setDoInput(true);
                    connection.setConnectTimeout(10000);
                    connection.setReadTimeout(10000);
                    connection.connect();
                    if (connection.getResponseCode() == 200) {
                        final InputStream is = connection.getInputStream();

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                pdfview.fromStream(is)//从输入流中加载pdf
                                        .defaultPage(pageNumber)//默认页数
                                        .onPageChange(new OnPageChangeListener() {
                                            @Override
                                            public void onPageChanged(int page, int pageCount) {
                                                pageNumber = page;
                                                mPageTv.setText(String.format("%s %s / %s", "", page + 1, pageCount));
                                            }
                                        })
                                        .enableAnnotationRendering(true)
                                        .onLoad(new OnLoadCompleteListener() {
                                            @Override
                                            public void loadComplete(int nbPages) {

                                            }
                                        })
                                        .scrollHandle(new DefaultScrollHandle(PdfViewerActivity.this))
                                        .spacing(10) // in dp
                                        .load();
                            }
                        });
                    } else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                ToastUtil.show(PdfViewerActivity.this, "加载失败");
                            }
                        });
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
